loader.define(function (require, exports, module) {


    var pageview = {},      // 页面的模块, 包含( init,bind )
        uiPullrefresh,      // 消息,电话公用的下拉刷新控件
        mainHeight,
        uiMask,             // 公共遮罩
        uiListviewMessage,  // 消息侧滑菜单
        uiListviewPhone,    // 通话记录侧滑菜单
        uiDropdownMore,     // 下拉菜单更多
        uiSlideTabMessage;  // 顶部tab

    /**
     * [init 页面初始化]
     * @return {[type]} [description]
     */
    pageview.init = function () {

        mainHeight = $(window).height() - $("#tab-home-header").height() - $("#tabDynamicNav").height();
        var slideHeight = parseInt(mainHeight) - $(".bui-searchbar").height();

        /*------------消息 电话 start --------------*/

        //初始化顶部TAB
        uiSlideTabMessage = bui.slide({
            id: "#tabMessage",
            menu: "#tabMessageNav",
            children: ".bui-tab-main ul",
            height: slideHeight,
            swipe: false,   //不允许通过滑动触发
            animate: false    //点击跳转时不要动画
        });
        //初始化消息的侧滑菜单
        uiListviewMessage = bui.listview({
            id: "#listview",
            data: [
                {text: "置顶", classname: "primary"},
                {text: "删除", classname: "danger"}
            ],
            width: 200,
            callback: function (e, ui) {

                var text = $(this).text().trim();

                if (text == '删除') {
                    // 给父层增加一个样式,便于控制删除
                    $(this).parents(".list-item").fadeOut(300, function () {
                        $(this).remove();
                    });
                }

                // 关闭侧滑
                ui.close();
            }
        });

        //初始化通话记录侧菜单
        uiListviewPhone = bui.listview({
            id: "#phonehistory",
            data: [{text: "删除", classname: "danger"}],
            callback: function (e, ui) {

                var text = $(this).text().trim();

                if (text == '删除') {
                    $(this).parents(".list-item").fadeOut(300, function () {
                        $(this).remove();
                    });
                }
                // 关闭侧滑
                ui.close();
            }
        });

        // 初始化下拉刷新
        uiPullrefresh = bui.pullrefresh({
            id: "#messageScroll",
            height: mainHeight,
            onRefresh: getData
        });

        /*------------消息 电话 end --------------*/

        /*------------右上角更多菜单 start --------------*/

        // 初始化下拉更多操作
        uiDropdownMore = bui.dropdown({
            id: "#more",
            showArrow: true,
            width: 160
        });
        // 下拉菜单有遮罩的情况
        uiMask = bui.mask({
            appendTo: "#main",
            opacity: 0.03,
            zIndex: 9,
            callback: function (argument) {
                // 隐藏下拉菜单
                uiDropdownMore.hide();
            }
        });
        // 通过监听事件绑定
        uiDropdownMore.on("show", function () {
            uiMask.show();
        })
        uiDropdownMore.on("hide", function () {
            uiMask.hide();
        });

        /*------------右上角更多菜单 end --------------*/


    }

    pageview.bind = function (argument) {

    }


    // 下拉刷新以后执行数据请求
    function getData() {

        myAjax({
            //几个参数需要注意一下
            method: "GET",//方法类型
            dataType: "json",//预期服务器返回的数据类型
            url: "userList",//url
            success: function (result) {
                uiPullrefresh.reverse();
                // 处理用户列表
                let data = result.data;
                let listview = $("#listview");
                $.each(data, function (index, value) {
                    let listDom = showUserList(value);
                    listview.append(listDom);
                });
            },
            error: function (result) {
                uiPullrefresh.fail();
            }
        })

        // bui.ajax({
        //     url : "http://www.easybui.com/demo/json/userlist.json",
        //     data: {
        //         pageindex:1,
        //         pagesize:4
        //     }
        // }).done(function(res) {
        //
        //     //还原刷新前状态
        //     uiPullrefresh.reverse();
        //
        // }).fail(function (res) {
        //     //请求失败变成点击刷新
        //     uiPullrefresh.fail();
        // })
    }

    function showUserList(data){
        let html = "";
        html +=
            '<li class="list-item">\n' +
            '   <div class="bui-btn bui-box" href="pages/chat/chat.html">\n' +
            '       <div class="ring ring-group">\n' +
            '           <i class="icon">&#xe675;</i>\n' +
            '       </div>\n' +
            '       <div class="span1">\n' +
            '           <h3 class="item-title">\n' +
            '               名字<span class="item-time bui-right">时间</span>\n' +
            '           </h3>\n' +
            '           <p class="item-text">内容\n' +
            '               <span class="bui-badges bui-right">未读消息数目</span>' +
            '           </p>' +
            '       </div>\n' +
            '   </div>\n' +
            '</li>';
        let $html = $(html);
        $html.find(".bui-btn").click(function () {
            sessionStorage.setItem('chatUser', data.username);
        });
        let itemTitle = $html.find(".item-title");
        itemTitle.html(itemTitle.html().replace("名字", data.username)
            .replace("时间", +data.lastMsgTime||'很早'));
        let itemText = $html.find(".item-text");
        itemText.html(itemText.html().replace("内容", data.unReadMsg||"")
            .replace("未读消息数目", +data.unReadMsgCount));
        if ($html.find(".item-text span").text() === "0") {
            $html.find(".item-text span").remove();
        }
        return $html;

        let li = $('<li class="list-item"></li>');
        let div = $('<div class="bui-btn bui-box" href="pages/chat/chat.html" ' +
            'onclick="sessionStorage.setItem(\'chatUser\', \''+data.username+'\')"></div>');
        let div1 = $('<div class="ring ring-group"><i class="icon">&#xe675;</i></div>');
        let div2 = $('<div class="span1"></div>');
        let div2_1 = $('<h3 class="item-title">'+data.username+'<span class="item-time bui-right">'+data.lastMsgTime||'很早'+'</span></h3>');
        let div2_2 = $('<p class="item-text"></p>');
        div2_2.text("123");
        // 未读消息数目
        let div2_2_1 = $('<span class="bui-badges bui-right">3</span>');
        div2_2.append(div2_2_1);
        div2.append(div2_1);
        div2.append(div2_2);
        div.append(div1);
        div.append(div2);
        li.append(div);
        return li;
    }

    // 控件初始化
    pageview.init();

    // 输出模块
    module.exports = pageview;
})